package com.htfyun.roobofactory.htfyunUtils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.util.Log;

import com.htfyun.roobofactory.R;

/**
 * Created by sst on 2018/7/7.
 */

public class BatteryInfo {

    private final Context mContext;

    private int batteryTemperature = 0;
    private int batteryLevel = 0;
    private int batteryVoltage = 0;
    private String batteryStatusString = "";
    private int batteryStatus = -1;
    private String batteryChargePlugString = "";
    private int batteryChargePlug = -1;
    private String batteryHealthString = "";
    private int batteryHealth = -1;


    private OnBatteryChangedListener mListener;

    public BatteryInfo(Context ctx) {
        mContext = ctx;
    }

    public void setOnBatteryChangedListener(OnBatteryChangedListener listener) {
        this.mListener = listener;
    }

    public void startMonitor() {
        mContext.registerReceiver(mBatteryReceiver, new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED));
    }

    public void stopMonitor() {
        mContext.unregisterReceiver(mBatteryReceiver);
    }

    public int getBatteryTemperature() {
        return batteryTemperature;
    }

    public int getBatteryLevel() {
        return batteryLevel;
    }

    public int getBatteryVoltage() {
        return batteryVoltage;
    }

    public String getBatteryStatusString() {
        return batteryStatusString;
    }

    public String getBatteryChargePlugString() {
        return batteryChargePlugString;
    }

    public String getBatteryHealthString() {
        return batteryHealthString;
    }

    public int getBatteryStatus() {
        return batteryStatus;
    }

    public int getBatteryChargePlug() {
        return batteryChargePlug;
    }

    public int getBatteryHealth() {
        return batteryHealth;
    }

    private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            batteryVoltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);

            batteryTemperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);

            int level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
            int scale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
            batteryLevel = (int)(((float)level / scale) * 100);

            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
            batteryStatus = status;
            int statusResId = R.string.battery_status_unknown;
            switch (status) {
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    statusResId = R.string.battery_status_charging;
                    break;
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    statusResId = R.string.battery_status_discharging;
                    break;
                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                    statusResId = R.string.battery_status_not_charging;
                    break;
                case BatteryManager.BATTERY_STATUS_FULL:
                    statusResId = R.string.battery_status_full;
                    break;
            }
            batteryStatusString = mContext.getResources().getString(statusResId);

            // 怎么充
            int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
            batteryChargePlug = chargePlug;
            Log.e("tag", "chargePlug = " + chargePlug);
            int chargePlugResId = R.string.battery_plugged_unknown;
            switch (chargePlug) {
                case BatteryManager.BATTERY_PLUGGED_AC:
                    chargePlugResId = R.string.battery_plugged_ac;
                    break;
                case BatteryManager.BATTERY_PLUGGED_USB:
                    chargePlugResId = R.string.battery_plugged_usb;
                    break;
                case BatteryManager.BATTERY_PLUGGED_WIRELESS:
                    chargePlugResId = R.string.battery_plugged_wireless;
                    break;
            }
            batteryChargePlugString = mContext.getResources().getString(chargePlugResId);


            int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_UNKNOWN);
            batteryHealth = health;
            int healthResId = R.string.battery_health_unknown;
            switch (health) {
                case BatteryManager.BATTERY_HEALTH_GOOD:
                    healthResId = R.string.battery_health_good;
                    break;
                case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                    healthResId = R.string.battery_health_overheat;
                    break;
                case BatteryManager.BATTERY_HEALTH_DEAD: // 未充电时就会显示此状态，这是什么鬼？
                    healthResId = R.string.battery_health_dead;
                    break;
                case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                    healthResId = R.string.battery_health_over_voltage;
                    break;
                case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                    healthResId = R.string.battery_health_unknown;
                    break;
                case BatteryManager.BATTERY_HEALTH_COLD:
                    healthResId = R.string.battery_health_cold;
                    break;
            }
            batteryHealthString = mContext.getString(healthResId);

            if (mListener != null) {
                mListener.onBatteryChangedListener();
            }

        }
    };

    public interface OnBatteryChangedListener {
        void onBatteryChangedListener();
    }



}
